{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "answers_path = \"answers.json\"\n",
    "train_path = \"train.json\"\n",
    "test_path = \"test.json\"\n",
    "valid_path = \"valid.json\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(answers_path) as f:\n",
    "    answers = json.load(f)\n",
    "    \n",
    "with open(train_path) as f:\n",
    "    trains = json.load(f)\n",
    "    \n",
    "with open(test_path) as f:\n",
    "    test = json.load(f)\n",
    "    \n",
    "with open(train_path) as f:\n",
    "    valid = json.load(f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "docs = []\n",
    "for k, item in answers.items():\n",
    "    docs.append(item[\"zh\"].strip())\n",
    "\n",
    "for k, item in trains.items():\n",
    "    docs.append(item[\"zh\"].strip())\n",
    "\n",
    "for k, item in test.items():\n",
    "    docs.append(item[\"zh\"].strip())\n",
    "\n",
    "for k, item in valid.items():\n",
    "    docs.append(item[\"zh\"].strip())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "import jieba\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "# process\n",
    "docs_ = []\n",
    "for doc in docs:\n",
    "    s = re.sub(u'[’!\"#$%&\\'()*+,-./:;<=>?@[\\\\]^_`{|}~]+', '', doc)\n",
    "    docs_.append(jieba.lcut(s))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gensim.models import Word2Vec"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = Word2Vec(docs_, sg=1, size=100,  window=5,  min_count=5,  negative=3, sample=0.001, hs=1, workers=4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.5/dist-packages/gensim/matutils.py:737: FutureWarning: Conversion of the second argument of issubdtype from `int` to `np.signedinteger` is deprecated. In future, it will be treated as `np.int64 == np.dtype(int).type`.\n",
      "  if np.issubdtype(vec.dtype, np.int):\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[('义务', 0.7068178653717041),\n",
       " ('金融', 0.6000909805297852),\n",
       " ('债务', 0.5874881744384766),\n",
       " ('意向', 0.5848835110664368),\n",
       " ('未来', 0.5829031467437744),\n",
       " ('慈善', 0.5410479307174683),\n",
       " ('意图', 0.5387165546417236),\n",
       " ('暂时性', 0.5328273177146912),\n",
       " ('暂时', 0.5311621427536011),\n",
       " ('捍卫', 0.5168660879135132)]"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.wv.most_similar(['负债'], topn=10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py:1: DeprecationWarning: Call to deprecated `__contains__` (Method will be removed in 4.0.0, use self.wv.__contains__() instead).\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "'金融aa' in model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
